﻿<?php
/**
 * Notice有关逻辑信息
 * @file NoticeModel.class.php
 * @auther liyangguang
 * @date 2012-01-06
 *
 */

class NoticeModel
{
    const NOTICE_NEW_CHEERLEADER = 1;
    const NOTICE_NEW_TRAINER_BID = 2;
    const NOTICE_AWARD_TRAINER   = 3;
    const NOTICE_REVIEW_TRAINER  = 4;
    const NOTICE_RECEIVE_AWARD   = 5;
    const NOTICE_RECEIVE_REVIEW  = 6;
    //TODO:
    const EMAIL_TYPE = 1;
    
    public function __construct()
    {
        $this->_objNoticesTable = new NoticesTable();
        $this->_objEmailsTable  = new EmailsTable();
        $this->_objEmailTemplatesTable = new EmailTemplatesTable();
        $this->_objUsersTable = new UsersTable();
        $this->_objBidsTable  = new BidsTable();
        $this->_objGoalsTable = new GoalsTable();
    }
    
    public function insertNewCheerLeaderNotice($intUID, $intCLUID, $intGID, $strCheerContent)
    {
        
        $arrData = array(
            'type' => NoticeModel::NOTICE_NEW_CHEERLEADER,
            'uid'  => intval($intUID),
            'status' => 0,
            'content' => ''.$intCLUID,
            'created_time' => time(),
        );
        
        $this->_objNoticesTable->insert($arrData);
        //email
        $objCheerLeader = $this->_objUsersTable->findUserWithID($intCLUID);
        $objClient      = $this->_objUsersTable->findUserWithID($intUID);
        $arrData = array(
            'cheerleader_firstname' => $objCheerLeader->firstname,
            'client_firstname'      => $objClient->firstname,
            'cheer_content'         => $strCheerContent,
            'goal_url'              => FITWOW_WEB_SITE . '/goal/detail/gid/'.$intGID,    
        );
        $arrToAddr = array($objClient->email);
        $this->insertNewEmail(9, $arrToAddr, $arrData);
    }
    
    public function insertNewTrainerBidNotice($intUID, $intBID)
    {
        $arrData = array(
            'type' => NoticeModel::NOTICE_NEW_TRAINER_BID,
            'uid'  => intval($intUID),
            'status' => 0,
            'content' => ''.$intBID,
            'created_time' => time(),
        );
        
        $this->_objNoticesTable->insert($arrData);
        $objUser = $this->_objUsersTable->findUserWithID($intUID);
        $objBid = $this->_objBidsTable->getBidWithBID($intBID);
        if ($objUser != null && $objBid != null && !empty($objUser->email)) {
            $objTrainer = $this->_objUsersTable->findUserWithID($objBid->uid);
            $arrData = array(
                'trainer_firstname' => $objTrainer->firstname,
                'trainer_lastname'  => $objTrainer->lastname,
                'client_firstname'  => $objUser->firstname,
                'bid_url'           => FITWOW_WEB_SITE . '/goal/biddetail/bid/'.$intBID,   
            );
            $arrToAddr = array($objUser->email);
            $this->insertNewEmail(1, $arrToAddr, $arrData);
        }
    }
    
    public function insertAwardTrainerNotice($intUID, $intGID) 
    {
        $arrData = array(
            'type' => NoticeModel::NOTICE_AWARD_TRAINER,
            'uid'  => intval($intUID),
            'status' => 0,
            'content' => ''.$intGID,
            'created_time' => time(),
        );
        
        $this->_objNoticesTable->insert($arrData);
        
    }
    
    public function insertReviewTrainerNotice($intUID, $intBID)
    {
        $arrData = array(
            'type' => NoticeModel::NOTICE_REVIEW_TRAINER,
            'uid'  => intval($intUID),
            'status' => 0,
            'content' => ''.$intBID,
            'created_time' => time(),
        );
        
        
        $this->_objNoticesTable->insert($arrData);
        $objClient = $this->_objUsersTable->findUserWithID($intUID);
        $objBid    = $this->_objBidsTable->getBidWithBID($intBID);
        if ($objClient != null && $objBid != null && !empty($objClient->email)) {
            $objTrainer = $this->_objUsersTable->findUserWithID($objBid->uid);
            $arrData = array(
                'trainer_firstname' => $objTrainer->firstname,
                'trainer_lastname'  => $objTrainer->lastname,
                'client_firstname'  => $objClient->firstname,
                'bid_url'           => FITWOW_WEB_SITE . '/goal/biddetail/bid/'.$intBID,   
            );
            $arrToAddr = array($objClient->email);
            $this->insertNewEmail(4, $arrToAddr, $arrData);
        }
    }
    public function insertAcceptAwardNotice($intUID, $intBID)
    {
        $arrData = array(
            'type' => NoticeModel::NOTICE_RECEIVE_AWARD,
            'uid'  => intval($intUID),
            'status' => 0,
            'content' => ''.$intBID,
            'created_time' => time(),
        );
        
        $this->_objNoticesTable->insert($arrData);
        
        $objTrainer = $this->_objUsersTable->findUserWithID($intUID);
        $objBid    = $this->_objBidsTable->getBidWithBID($intBID);
        $objGoal   = $this->_objGoalsTable->getGoalWithGID($objBid->gid);
        if ($objTrainer != null && $objGoal != null && !empty($objTrainer->email)) {
            $objClient = $this->_objUsersTable->findUserWithID($objGoal->uid);
            $arrData = array(
                'trainer_firstname' => $objTrainer->firstname,
                'client_firstname'  => $objClient->firstname,
                'client_lastname'  => $objClient->lastname,
                'bid_url'           => FITWOW_WEB_SITE . '/goal/biddetail/bid/'.$objGoal->selected_bid,   
            );
            $arrToAddr = array($objTrainer->email);
            $this->insertNewEmail(3, $arrToAddr, $arrData);
        }
    }
    
    public function insertReceiveReviewNotice($intUID, $intBID)
    {
        $arrData = array(
            'type' => NoticeModel::NOTICE_RECEIVE_REVIEW,
            'uid'  => intval($intUID),
            'status' => 0,
            'content' => ''.$intBID,
            'created_time' => time(),
        );
        $this->_objNoticesTable->insert($arrData);
        
        $objTrainer = $this->_objUsersTable->findUserWithID($intUID);
        $objBid    = $this->_objBidsTable->getBidWithBID($intBID);
        $objGoal   = $this->_objGoalsTable->getGoalWithGID($objBid->gid);
        if ($objTrainer != null && $objGoal != null && !empty($objTrainer->email)) {
            $objClient = $this->_objUsersTable->findUserWithID($objGoal->uid);
            $arrData = array(
                'trainer_firstname' => $objTrainer->firstname,
                'client_firstname'  => $objClient->firstname,
                'client_lastname'  => $objClient->lastname,
                'bid_url'           => FITWOW_WEB_SITE . '/goal/detail/gid/'.$objGoal->id,   
            );
            $arrToAddr = array($objTrainer->email);
            $this->insertNewEmail(6, $arrToAddr, $arrData);
        }
    }
    
    
    
    public function setNoticeRead($intNID) 
    {
        $arrData = array(
            'status' => 1,
        );
        $intNID = intval($intNID);
        return $this->_objNoticesTable->updateNotice($intNID, $arrData);
    }
    
    public function getNoReadNoticeListWithUID($intUID)
    {
        $arrResult = $this->_objNoticesTable->getNoReadNoticeListWithUID($intUID);
        foreach ($arrResult as &$row) {
            switch($row['type']) {
                case NoticeModel::NOTICE_NEW_CHEERLEADER : 
                    $row['url'] = '/user/detail/uid/'.$row['content'];
                    $row['content'] = '! You got '.$row['cnt'].' new cheerleader';
                    break;
                case NoticeModel::NOTICE_NEW_TRAINER_BID:
                    $row['url'] = '/goal/biddetail/bid/'.$row['content'];
                    $row['content'] = '! '.$row['cnt'].' new trainer bid';
                    break;
                case NoticeModel::NOTICE_AWARD_TRAINER:
                    $row['url'] = '/goal/detail/gid/'.$row['content'];
                    $row['content'] = '! Bid is over, please award trainer';
                    break;
                case NoticeModel::NOTICE_REVIEW_TRAINER:
                    $row['url'] = '/goal/biddetail/bid/'.$row['content'];
                    $row['content'] = '! Congratulations! Trainer has accepted your award, don’t forget to leave him a review soon';
                    break;
                case NoticeModel::NOTICE_RECEIVE_AWARD:
                    $row['url'] = '/goal/biddetail/bid/'.$row['content'];
                    $row['content'] = '! Congratulations! Client award you , please contact the client';
                    break;
                case NoticeModel::NOTICE_RECEIVE_REVIEW:
                    $row['url'] = '/goal/biddetail/bid/'.$row['content'];
                    $row['content'] = '! new review received';
                    break;
            }
        }
        return $arrResult;
    }
    
    public function insertNewEmail($intType, $arrToAddr, $arrData)
    {       
        $intType = intval($intType);
        $objEmailTemplate = $this->_objEmailTemplatesTable->getEmailTemplateWithEID($intType);
        if ($objEmailTemplate == null) {
            return false;
        }
        switch($intType) {
            case 1  :
                $arrSearch = array('{*$trainer_firstname*}', '{*$trainer_lastname*}', '{*$client_firstname*}', '{*$bid_url*}');
                $strSubject = str_replace($arrSearch, $arrData, $objEmailTemplate->subject_template);
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 3  :
                $arrSearch = array('{*$trainer_firstname*}', '{*$client_firstname*}', '{*$client_lastname*}', '{*$bid_url*}');
                $strSubject = str_replace($arrSearch, $arrData, $objEmailTemplate->subject_template);
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 4  :
                $arrSearch = array('{*$trainer_firstname*}', '{*$trainer_lastname*}', '{*$client_firstname*}', '{*$bid_url*}');
                $strSubject = str_replace($arrSearch, $arrData, $objEmailTemplate->subject_template);
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 6  :
                $arrSearch = array('{*$trainer_firstname*}', '{*$client_firstname*}', '{*$client_lastname*}', '{*$bid_url*}');
                $strSubject = str_replace($arrSearch, $arrData, $objEmailTemplate->subject_template);
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 7  :
                $arrSearch = array('{*$client_firstname*}', '{*$password_url*}');
                $strSubject = $objEmailTemplate->subject_template;
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 8 : 
                $arrSearch = array('{*$client_firstname*}', '{*$goal_url*}');
                $strSubject = $objEmailTemplate->subject_template;
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 9 : 
                $arrSearch = array('{*$cheerleader_firstname*}','{*$client_firstname*}', '{*$cheer_content*}', '{*$goal_url*}');
                $strSubject = str_replace($arrSearch, $arrData, $objEmailTemplate->subject_template);
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
            case 10 : 
                $arrSearch = array('{*$client_firstname*}', '{*$goal_url*}');
                $strSubject = $objEmailTemplate->subject_template;
                $strContent = str_replace($arrSearch, $arrData, $objEmailTemplate->content_template);
                break;
        }
        $arrEmailData = array(
            'subject' => $strSubject,
            'content' => $strContent,
            'status'  => 0,
            'created_time' => time(),
        );
        foreach ($arrToAddr as $strToAddr) {
            $arrEmailData['to_addr'] = $strToAddr;
            $this->_objEmailsTable->insertNewEmail($arrEmailData);
        }
        return true;
    }
    
    protected $_objNoticesTable;
    protected $_objEmailsTable;
    protected $_objEmailTemplatesTable;
    protected $_objUsersTable;
    protected $_objBidsTable;
    protected $_objGoalsTable;
}
 
?>